﻿package com.xuanque.mahjongDice.display
{
	import utils.transitions.*;
	//import com.xuanque.mahjongDice.data.Player;
	import datas.Player;
	
	import com.xuanque.mahjongDice.ui.QcontextMenu;
	import flash.events.MouseEvent;
	
	import flash.display.*;
	import flash.text.TextField;
	import flash.system.ApplicationDomain;
	import utils.HallConfig;
	
	public class PlayerItem extends Sprite {
		
		
		private var list_text:String;
		private var click_btn:Sprite;
		
		public function PlayerItem(player:Player){
			this.list_text = player.getLoginName();
			
			createChildren();
			addListener();
			
			//玩家会员等级
			var vipClass:Class = HallConfig.getIntance().getApplication().getDefinition("com.member" + player.getMember() ) as Class;
			var member_mc:MovieClip = new vipClass;
			member_mc.x = 5;
			member_mc.scaleX = member_mc.scaleY = .8;
			addChild(member_mc);
			
			this.setText(player);
		}
		
		/*
		 * 设置信息 
		*/
		public function setText(player:Player):void {
			
			//加入的玩家信息
			
			var roomStatus:String; //.所处房间状态	byte  1  0正在游戏中   1非游戏中  2.准备  5.重连
			switch(player.getRoomStatus()) {
				
				case 0:
					roomStatus = "游戏中";
					break;
				case 1:
					roomStatus = "闲置中";
					break;
				case 2:
					roomStatus = "准备中";
					break;
				case 5:
					roomStatus = "重连中";
					break;
			}
			this.roomStatus_txt.text = roomStatus;
			
			//this.nickName_txt.text = String(player.getLoginName());
			this.nickName_txt.text = String(player.getNickName());
			this.sex_txt.text = player.getSex()==0?"男":"女";
			this.grading_txt.text = String(player.getGrading());
			this.rice_txt.text = String(player.getRice());
			this.paddy_txt.text = String(player.getPaddy());
			this.seed_txt.text = String(player.getSeed());
			
			this.joinTimes_txt.text = String(player.getTotalNum());
			this.win_txt.text = String(player.getWinNum());
			this.lose_txt.text = String(player.getLoseNum());
			this.draw_txt.text = String(player.getPushNum());
			
			this.outTimes_txt.text = String(player.getOutNum());
			this.playTimes_txt.text = String(player.getPlayTime());
			this.room_txt.text = String(player.getTableNum());
			
			bulidContextMenu(player , roomStatus);
		}
		
		
		private function createChildren():void {
			this.click_btn = new Sprite();
			this.click_btn.alpha = 0.5;
			
			this.drawShape(click_btn, this.width + 30, this.height, 0x000000);
			addChild(click_btn);
		}
		
		private function addListener():void{
			click_btn.addEventListener(MouseEvent.ROLL_OVER,eventHandler);
			click_btn.addEventListener(MouseEvent.ROLL_OUT, eventHandler);
			click_btn.addEventListener(MouseEvent.CLICK, eventHandler);
		}
		private function eventHandler(event:MouseEvent):void {
			var _btn:Sprite = event.target as Sprite;
			
			switch(event.type) {
				case MouseEvent.ROLL_OVER:
				   Tweener.addTween(_btn,{alpha:1,time:.5});
					break;
					
				case MouseEvent.ROLL_OUT:
					Tweener.addTween(_btn,{alpha:0.5,time:.5});
					break;
					
				case MouseEvent.CLICK:
					
					break;
				
			}
			
			
		}
		
		private function bulidContextMenu(player:Player ,roomStatus:String ):void{
			this.contextMenu = QcontextMenu.getInstance().bulidPlayerInfo(player , roomStatus);
		}
		
		/**
		 * 用户名
		 * @return 
		 * 
		 */		
		public function get _text():String{
			return this.list_text;
		} 
		
		
		
		/**
		* 主要为画背景,用于过渡的效果;
		* @param	mc
		* @param	w
		* @param	clr
		* @param	alp
		*/
		private function drawShape(mc:Sprite, w:int,h:int,color:Number, alpha:Number = 0.1) {
			mc.graphics.beginFill(color, alpha);
			mc.graphics.drawRect(0, 0, w, h);
			mc.graphics.endFill();
		}
		
		
		
	}
}